<?php 		
class CTestimonialController extends CController{

	protected $m_arrobjTestimonials;
	protected $m_objTestimonial;
	protected $m_objFCKeditor;
		
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();		
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case 'add':									
			case 'edit':
				$this->handleAddEditTestimonial();
				break;
				
			case 'insert_update_testimonial':
				$this->handleInsertUpdateTestimonial();
				break;
			
			case 'update_testimonial_action':
				$this->handleUpdateTestimonialAction();
				break;
			
			case 'view':
				$this->handleViewTestimonial();
				break;
				
			default:
				$this->handleViewTestimonialList();
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewTestimonialList() {
		
		$this->m_arrobjTestimonials = CTestimonials::fetchTestimonials( $this->m_objDatabase );
		
		$this->displayTestimonialList();
	}
	
	function handleAddEditTestimonial() {
		
		include( "Liabraries/fckeditor/fckeditor.php");
		
		$this->m_objFCKeditor = new FCKeditor('testimonial[description]') ;
		$this->m_objFCKeditor->BasePath = SITE_PATH . '/Liabraries/fckeditor/';		
		$this->m_objFCKeditor->Width  = '100%';
		$this->m_objFCKeditor->Height = '500';
		$intTestimonialId = $this->m_objData->getRequestData( array( 'id' ) );
		$this->m_objTestimonial = new CForum();
		if( true == isset( $intTestimonialId ) ) {
			$this->m_objTestimonial = $this->viewTestimonialById( $intTestimonialId );
			$this->m_objFCKeditor->Value  = $this->m_objTestimonial->getDescription();
		}
		
		$this->displayAddEditTestimonial();
	}

	function handleViewTestimonial() {
		
		$intTestimonialId = $this->m_objData->getRequestData( array( 'id' ) );		
		if( true == isset( $intTestimonialId ) ) {
			$this->m_objTestimonial = $this->viewTestimonialById( $intTestimonialId );
		}	
		$this->displayViewTestimonial();
	}
	
	function handleInsertUpdateTestimonial() {
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'testimonial' ) );
		
				$this->m_objTestimonial = CTestimonials::setValues( 'CTestimonial', $arrmixFieldsValues );
				
				if( false == $this->m_objTestimonial->validate( $arrmixFieldsValues, array( 'title', 'description' ) ) ) {
					break;
				}
				
				if( 0 < $arrmixFieldsValues['id'] ) {
				
					if( false == $this->m_objTestimonial->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update testimonial.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Testimonial update successfully. ' );
					}
				} else {
				
					if( false == $this->m_objTestimonial->insert( $this->m_objLoginUser->getId(), $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create Testimonial.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Testimonial created successfully. ' );
					}
				}
				redirect( '/admin/testimonial' );
		}
 		redirect( '/admin/testimonial/add' );
	}
	
	function handleUpdateTestimonialAction() {
		
		switch( true ) {
			default:
				
				$strAction = $this->m_objData->getPostData( array( 'testimonial', 'action' ) );				
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
				
				$arrintTestimonialIds = $this->m_objData->getPostData( array( 'testimonial', 'id' ) );
				if( false == valArr( $arrintTestimonialIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
								
				$this->m_objTestimonial = new CTestimonial();
				if( false == valObj( $this->m_objTestimonial, 'CTestimonial' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
				
				if( 'delete' == $strAction ) {
					if( true == $this->m_objTestimonial->deleteTestimonialsById( $arrintTestimonialIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objTestimonial->updateTestimonialsActionById( $strAction, $arrintTestimonialIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
									
		}
		redirect( '/admin/testimonial' );
	}
	
	/**************************************
	********** OTHER FUNCTIONS **********
	**************************************/
	
	function viewTestimonialById( $intTestimonialId ) {
		return CTestimonials::fetchTestimonialById( $intTestimonialId, $this->m_objDatabase );		
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayTestimonialList() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'testimonials', $this->m_arrobjTestimonials );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/testimonial/view_testimonial_list.tpl');
	}
	
	function displayAddEditTestimonial() {
		$this->display();
		
		$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'testimonial', $this->m_objTestimonial );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/testimonial/view_add_edit_testimonial.tpl' );
	}
	
	function displayViewTestimonial() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'testimonial', $this->m_objTestimonial );		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/testimonial/view_testimonial.tpl');

	}
	
	function display() {
		
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'testimonials' );
	}
}
?>